import { createWebHistory, createRouter } from 'vue-router'
/* Layout */
import Layout from '@/layout'
// import CheckInForm from '@/views/system/management/checkInForm.vue' // 引入页面组件
/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * roles: ['admin', 'common']       // 访问路由的角色权限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
 * meta : {
    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */

// 公共路由
export const constantRoutes = [
// 详情页路由配置
{
  path: '/service/arrangement-detail-executed',
  component: Layout,
  hidden: true,
  children: [
    {
      path: '',
      component: () => import('@/views/system/arrangement/detail-executed'),
      name: 'ArrangementDetailExecuted',
      meta: {
        title: '任务安排详情-已执行',
        activeMenu: '/system/arrangement'
      }
    }
  ]
},
{
  path: '/service/arrangement-detail-pending',
  component: Layout,
  hidden: true,
  children: [
    {
      path: '',
      component: () => import('@/views/system/arrangement/detail-pending'),
      name: 'ArrangementDetailPending',
      meta: {
        title: '任务安排详情-待执行',
        activeMenu: '/system/arrangement'
      }
    }
  ]
},
{
  path: '/service/arrangement-detail-cancelled',
  component: Layout,
  hidden: true,
  children: [
    {
      path: '',
      component: () => import('@/views/system/arrangement/detail-cancelled'),
      name: 'ArrangementDetailCancelled',
      meta: {
        title: '任务安排详情-已取消',
        activeMenu: '/system/arrangement'
      }
    }
  ]
},

  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  // 退住领导审批（按用户提供地址）
  // {
  //   path: '/tuizhucheshi/tuizhulingdaoshenpi',
  //   component: Layout,
  //   children: [
  //     {
  //       path: '',
  //       component: () => import('@/views/system/out_management/leaderApproval.vue'),
  //       name: 'CheckoutLeaderApproval',
  //       meta: { title: '申请审批', activeMenu: '/system/out_management' }
  //     }
  //   ]
  // },
  // 兼容用户访问地址（无 h）
  // {
  //   path: '/tuizhuceshi/tuizhulingdaoshenpi',
  //   component: Layout,
  //   children: [
  //     {
  //       path: '',
  //       component: () => import('@/views/system/out_management/leaderApproval.vue'),
  //       name: 'CheckoutLeaderApprovalAlias',
  //       meta: { title: '申请审批', activeMenu: '/system/out_management' }
  //     }
  //   ]
  // },
  // 解除合同（按用户提供地址）
  {
    path: '/tuizhuceshi/jiechuhetong',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/out_management/contractTermination.vue'),
        name: 'ContractTermination',
        meta: { title: '解除合同', activeMenu: '/system/out_management' }
      }
    ]
  },
  // 解除合同处理（按用户提供地址）
  {
    path: '/tuizhuceshi/jiechuhetongchuli',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/out_management/contractTerminationProcessing.vue'),
        name: 'ContractTerminationProcessing',
        meta: { title: '解除合同处理', activeMenu: '/system/out_management' }
      }
    ]
  },
  // 退款管理页面
  {
    path: '/Finance/ddgl/recordservice',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/recordservice/index.vue'),
        name: 'RefundManagement',
        meta: { title: '退款管理', activeMenu: '/system/recordservice' }
      }
    ]
  },
  // 余额查询页面
  {
    path: '/CaiwuManage/ycgl/balance',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/balance/index.vue'),
        name: 'BalanceInquiry',
        meta: { title: '余额查询', activeMenu: '/system/balance' }
      }
    ]
  },
  // 欠费老人页面
  {
    path: '/CaiwuManage/zdgl/qflr',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/overdueElderly/index.vue'),
        name: 'OverdueElderly',
        meta: { title: '欠费老人', activeMenu: '/system/overdueElderly' }
      }
    ]
  },
  // 账单详情（放到常量路由，避免首次点击时路由尚未动态注册导致白屏）
  {
    path: '/system/bill/detail',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        component: () => import('@/views/system/bill/detail'),
        name: 'BillDetail',
        meta: { title: '账单详情', activeMenu: '/system/bill' }
      }
    ]
  },
  // 合同详情页面路由配置
  {
    path: '/system/contract/xq',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        component: () => import('@/views/system/contract/xq.vue'),
        name: 'ContractDetail',
        meta: { title: '合同详情', activeMenu: '/system/contract' }
      }
    ]
  },
  {
    path: '/order/preview',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        component: () => import('@/views/system/ordermanagement/preview.vue'),
        name: 'OrderPreview',
        meta: { title: '订单查看', activeMenu: '/system/ordermanagement' }
      }
    ]
  },
  {
    path: '/system/management', // 入住管理主页面路由
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/management/index.vue'),
        name: 'Management',
        meta: { 
          title: '入住管理', 
          noCache: false, 
          keepAlive: true
        }
      }
    ]
  },
  {
    path: '/system/management/checkInForm', // 要跳转的 URL 路径
    component: Layout, // 嵌套在布局组件里（这样页面会有侧边栏、顶部栏）
    children: [
      {
        path: '',
        component: () => import('@/views/system/management/checkInForm.vue'),
        name: 'CheckIn',
        meta: { title: '发起入住申请' }
      }
    ]
  },
  {
    path: '/system/out_management', // 退住管理主页面路径
    component: Layout, // 嵌套在布局组件里（这样页面会有侧边栏、顶部栏）
    children: [
      {
        path: '',
        component: () => import('@/views/system/out_management/index.vue'),
        name: 'OutManagement',
        meta: { title: '退住管理' }
      }
    ]
  },
  {
    path: '/system/out_management/checkoutForm', // 退住申请表单路径
    component: Layout, // 嵌套在布局组件里（这样页面会有侧边栏、顶部栏）
    children: [
      {
        path: '',
        component: () => import('@/views/system/out_management/checkoutForm.vue'),
        name: 'CheckoutForm',
        meta: { title: '发起退住申请' }
      }
    ]
  },
  // 退住申请情页-申请退住（按用户提供地址）
  {
    path: '/tuizhuceshi/shenqingtuizhu',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/out_management/checkoutForm.vue'),
        name: 'CheckoutFormAlt',
        meta: { title: '申请退住', activeMenu: '/system/out_management' }
      }
    ]
  },
  // 退住申请页-申请审批（按用户提供地址）
  {
    path: '/tuizhuceshi/shenqingshenpi',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/out_management/approvalPending.vue'),
        name: 'CheckoutApprovalPending',
        meta: { title: '申请审批' }
      }
    ]
  },
  // 退住申请审批通过页面（按用户提供地址）
  {
    path: '/tuizhuceshi/tuizhushenpitongguo',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/approval-passed/index.vue'),
        name: 'CheckoutApprovalPassed',
        meta: { title: '退住申请审批通过' }
      }
    ]
  },
  // 审批被拒绝页面（按用户提供地址）
  {
    path: '/tuizhuceshi/spbjj',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/approval-rejected/index.vue'),
        name: 'CheckoutApprovalRejected',
        meta: { title: '审批被拒绝' }
      }
    ]
  },
  // 审批流程测试 - 申请审批页面（按用户提供地址 /splccs/spsq）
  {
    path: '/splccs/spsq',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/approval/index.vue'),
        name: 'ApprovalProcess',
        meta: { title: '审批申请', activeMenu: '/splccs' }
      }
    ]
  },
  // 审批流程测试 - 解除合同页面（按用户提供地址 /splccs/jcht）
  {
    path: '/splccs/jcht',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/contract-termination/index.vue'),
        name: 'ContractTerminationProcess',
        meta: { title: '解除合同', activeMenu: '/splccs' }
      }
    ]
  },
  // 审批流程测试 - 账单调整页面（按用户提供地址 /splccs/zdtz）
  {
    path: '/splccs/zdtz',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/bill-adjustment/index.vue'),
        name: 'BillAdjustmentProcess',
        meta: { title: '账单调整', activeMenu: '/splccs' }
      }
    ]
  },
  // 审批流程测试 - 审批申请通过页面（按用户提供地址 /splccs/spsqtg）
  {
    path: '/splccs/spsqtg',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/approval-passed/index.vue'),
        name: 'ApprovalPassedProcess',
        meta: { title: '审批申请通过', activeMenu: '/splccs' }
      }
    ]
  },
  // 审批流程测试 - 解除合同通过页面（按用户提供地址 /splccs/jchttg）
  {
    path: '/splccs/jchttg',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/contract-termination-passed/index.vue'),
        name: 'ContractTerminationPassedProcess',
        meta: { title: '解除合同通过', activeMenu: '/splccs' }
      }
    ]
  },
  // 审批流程测试 - 账单调整后页面（按用户提供地址 /splccs/zdtzh）
  {
    path: '/splccs/zdtzh',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/bill-adjustment-result/index.vue'),
        name: 'BillAdjustmentResultProcess',
        meta: { title: '账单调整后', activeMenu: '/splccs' }
      }
    ]
  },
  // 审批流程测试 - 账单调整后提款页面（按用户提供地址 /splccs/zdtzhtk）
  {
    path: '/splccs/zdtzhtk',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/bill-settlement/index.vue'),
        name: 'BillSettlementProcess',
        meta: { title: '账单调整后提款', activeMenu: '/splccs' }
      }
    ]
  },
  // 审批流程测试 - 副院长审批退住页面（按用户提供地址 /splccs/fyzsptz）
  {
    path: '/splccs/fyzsptz',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/system/vice-president-approval/index.vue'),
        name: 'VicePresidentApprovalProcess',
        meta: { title: '副院长审批退住', activeMenu: '/splccs' }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: () => import('@/views/index'),
        name: 'Index',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'profile/:activeTab?',
        component: () => import('@/views/system/user/profile/index'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user' }
      }
    ]
  }
]

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes = [
  {
    path: '/system/user-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:user:edit'],
    children: [
      {
        path: 'role/:userId(\\d+)',
        component: () => import('@/views/system/user/authRole'),
        name: 'AuthRole',
        meta: { title: '分配角色', activeMenu: '/system/user' }
      }
    ]
  },
  {
    path: '/system/role-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:role:edit'],
    children: [
      {
        path: 'user/:roleId(\\d+)',
        component: () => import('@/views/system/role/authUser'),
        name: 'AuthUser',
        meta: { title: '分配用户', activeMenu: '/system/role' }
      }
    ]
  },
  {
    path: '/system/dict-data',
    component: Layout,
    hidden: true,
    permissions: ['system:dict:list'],
    children: [
      {
        path: 'index/:dictId(\\d+)',
        component: () => import('@/views/system/dict/data'),
        name: 'Data',
        meta: { title: '字典数据', activeMenu: '/system/dict' }
      }
    ]
  },
  {
    path: '/monitor/job-log',
    component: Layout,
    hidden: true,
    permissions: ['monitor:job:list'],
    children: [
      {
        path: 'index/:jobId(\\d+)',
        component: () => import('@/views/monitor/job/log'),
        name: 'JobLog',
        meta: { title: '调度日志', activeMenu: '/monitor/job' }
      }
    ]
  },
  {
    path: '/tool/gen-edit',
    component: Layout,
    hidden: true,
    permissions: ['tool:gen:edit'],
    children: [
      {
        path: 'index/:tableId(\\d+)',
        component: () => import('@/views/tool/gen/editTable'),
        name: 'GenEdit',
        meta: { title: '修改生成配置', activeMenu: '/tool/gen' }
      }
    ]
  },
  // 在动态路由或静态路由中添加
  {
    path: '/system/sq',
    component: Layout,
    permissions: ['system:request:add'],
    children: [
      {
        path: '',
        component: () => import('@/views/system/sq/index'),
        name: 'LeaveApply',
        meta: { title: '请假申请', activeMenu: '/system/request' }
      }
    ]
  },
  // 添加请假详情页面路由
  { path: '/system/leave/detail', component: Layout, hidden: true, permissions: ['system:request:list'], children: [
      { path: '', component: () => import('@/views/system/leave/detail'), name: 'LeaveDetail', meta: { title: '请假详情', activeMenu: '/system/request' }}
    ]
  },
  // 添加账单详情页面路由
  // 已迁移到 constantRoutes，避免首次导航白屏
]

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    }
    return { top: 0 }
  },
})

// 路由守卫已移至 permission.js 中统一处理

export default router
